/**
* @author Lei Gao
*/
import java.io.*;
import java.net.*;
import java.util.*;
import java.io.IOException;

public class HttpGet {

private static int BUFFER_SIZE = 8096;
private Vector vDownLoad = new Vector();//URL list
private Vector vFileList = new Vector();//save name list

public HttpGet() {
}
public void resetList() {
vDownLoad.clear();
vFileList.clear();
}

public void addItem(String url, String filename) {
vDownLoad.add(url);
vFileList.add(filename);
}
/**
* Download by list
*/
public void downLoadByList() {

String url = null;
String filename = null;

for(int i=0;i<vDownLoad.size();i++){
url = (String) vDownLoad.get(i);
filename = (String) vFileList.get(i);
try {
saveToFile(url, filename);
}
catch (IOException err) {

System.out.println("resource[" + url + "]Download failed!!!");

}

}
		

System.out.println("Download completed!!!");

		}



/**
* save as...
*
*/
public void saveToFile(String destUrl, String fileName) throws IOException {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;

//connecting...
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//link to url
httpUrl.connect();
//get url input stream
bis = new BufferedInputStream(httpUrl.getInputStream());
//file
fos = new FileOutputStream(fileName);

System.out.println("access link[" + destUrl + "]...\n saved as [" + fileName + "]");
//save
while ( (size = bis.read(buf)) != -1)
fos.write(buf, 0, size);

fos.close();
bis.close();
httpUrl.disconnect();
}


}